package com.dupf.product.rpc.service.impl;

import com.dupf.common.util.CommonThreadPool;
import com.dupf.common.util.RedisUtil;
import com.dupf.common.util.Tools;
import com.dupf.product.common.constant.ProductConstant;
import com.dupf.product.dao.mapper.ProductCategoryMapper;
import com.dupf.product.dao.mapper.ProductCollectRecordMapper;
import com.dupf.product.dao.mapper.ProductDetailMapper;
import com.dupf.product.dao.mapper.ProductInfoMapper;
import com.dupf.product.dao.model.ProductCollectRecord;
import com.dupf.product.dao.model.ProductDetail;
import com.dupf.product.dao.model.ProductInfo;
import com.dupf.product.dao.smodel.ProductCollectParamDTO;
import com.dupf.product.rpc.api.ProductCategoryService;
import com.dupf.product.rpc.api.ProductCollectService;
import com.qiniu.storage.model.FetchRet;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ProductCollectRecordService实现
 * Created by dupf on 2018/3/14.
 */
@Service
@Transactional
public class ProductCollectServiceImpl implements ProductCollectService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductCollectServiceImpl.class);

    private static final String DRIVER_CHROME_COOKIE_KEY = "driver:chrome:cookie";
    private static final List<String> SIZE_CONSTANT = Arrays.asList("size", "规格", "尺寸", "尺码", "容量", "含量", "长度", "宽度", "浓度", "高", "低", "长", "宽", "大小");
    private static final String DELIMITER = "##";

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductCollectRecordMapper productCollectRecordMapper;

    @Autowired
    private QiniuUploadServiceImpl qiniuUploadServiceImpl;
    @Autowired
    private ProductCategoryService productCategoryService;

    @Override
    public void newCollectionTask(int recordId, int userId, int orgId, String category, String categoryId, String sourceUrl, int type) {

        ProductCollectRecord record = new ProductCollectRecord();
        record.setId(recordId);
        try {
            if (type == 1) {
            } else {
                executeCollectBatch(userId, orgId, category, categoryId, sourceUrl);
            }

            record.setStatus((byte) 1);
        } catch (Exception e) {
            LOGGER.error("采集产品报错 url：" + sourceUrl, e);
            record.setStatus((byte) 2);
        } finally {
            record.setEndTime(System.currentTimeMillis());
            productCollectRecordMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public int collectProductByContent(ProductCollectParamDTO paramDTO) {
        ProductCollectRecord record = new ProductCollectRecord();
        record.setId(paramDTO.getRecordId());

        int result = 0;
        try {

            executeCollectSingleByContent(paramDTO);
            record.setStatus((byte) 1);
        } catch (Exception e) {
            LOGGER.error("采集产品报错 url：" + paramDTO.getUrl(), e);
            record.setStatus((byte) 2);
        } finally {
            record.setEndTime(System.currentTimeMillis());
            productCollectRecordMapper.updateByPrimaryKeySelective(record);
        }

        return result;
    }

    private void executeCollectSingleByContent(ProductCollectParamDTO paramDTO) throws MalformedURLException {

        String html = paramDTO.getContent();
        String sourceUrl = paramDTO.getUrl();
        Integer userId = paramDTO.getUserId();
        Integer orgId = paramDTO.getOrgId();
        String categoryId = paramDTO.getCategoryId();


        Document document = Jsoup.parse(html);

        ProductInfo productInfo = new ProductInfo();
        ProductDetail productDetail = new ProductDetail();
        productDetail.setStock(100);

        URL url = new URL(sourceUrl);
        String host = url.getHost();// 获取主机名
        Set<String> imageUrlQiniu = new HashSet<>();
        if (host.contains("detail.1688.com")) {
            parser1688Product(document, productInfo, productDetail, imageUrlQiniu);
        } else if (host.contains("item.taobao.com")) {
            parserTaobaoProduct(document, productInfo, productDetail, imageUrlQiniu);
        } else if (host.contains("detail.tmall.com")) {
            parserTmallProduct(document, productInfo, productDetail, imageUrlQiniu);
        } else if (host.contains("www.amazon")) {
            parserAmazonProduct(document, productInfo, productDetail, imageUrlQiniu);
        } else {
            return;
        }

        long currentTime = System.currentTimeMillis();

        productInfo.setCollectUserId(userId);
        productInfo.setOrgId(orgId);
        productInfo.setCtime(currentTime);
        productInfo.setOrders(currentTime);
        productInfo.setReleaseTime(currentTime);
        productInfo.setSourceDomain(host.substring(host.indexOf(".") + 1));
        productInfo.setCostCurUnit((byte) 1);
        productInfo.setSourceUrl(sourceUrl);

        productInfo.setSku(String.valueOf(RedisUtil.incrementAndGet("product_sku", 1525330L, null)));

        // 设置title
        productInfo.setTitle(productDetail.getTitleZh());

        // 主图
        if (!StringUtils.isEmpty(productDetail.getPicture())) {
            productInfo.setMainPic(productDetail.getPicture().split(",")[0]);
        }
        // 分类
        if (!StringUtils.isEmpty(categoryId)) {

            productInfo.setCategoryId(categoryId);
            productInfo.setCategory(productCategoryService.getCategoryByIds(categoryId));
        }

        // 产品编号
        productInfo.setProductNo(String.valueOf(RedisUtil.incrementAndGet("product_no", 15253L, null)));

        // 默认
        productInfo.setStatus((byte) 1);
        productInfo.setProductTag((byte) 3);
        productInfoMapper.insertSelective(productInfo);

        productDetail.setProductId(productInfo.getId());
        productDetail.setCtime(currentTime);
        productDetailMapper.insertSelective(productDetail);
        CommonThreadPool.execute(() -> {
            /**
             * 上传图片到七牛云
             */
            final ProductInfo productInfoUpdate = new ProductInfo();
            productInfoUpdate.setId(productInfo.getId());
            productInfoUpdate.setMainPic(productInfo.getMainPic());

            final ProductDetail productDetail1Update = new ProductDetail();
            productDetail1Update.setId(productDetail.getId());
            productDetail1Update.setPicture(productDetail.getPicture());
            productDetail1Update.setVariantCompose(productDetail.getVariantCompose());

            if (CollectionUtils.isNotEmpty(imageUrlQiniu)) {
                try {
                    imageUrlQiniu.forEach(item -> {
                        String variantCompose = productDetail1Update.getVariantCompose();
                        String key = "product" + "/" + userId + "/" + System.currentTimeMillis() + Tools.getZeroNineRand(3) + ".jpg";
                        try {

                            FetchRet fetchRet = qiniuUploadServiceImpl.uploadUrlFile(item, ProductConstant.QINIU_BUCKET_PICTURE, key);
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                LOGGER.info("休眠1.5秒钟");
                            }
                            if (fetchRet != null) {
                                String newUrl = ProductConstant.QINIU_BUCKET_PICTURE_DOMAIN + "/" + key;

                                if (!StringUtils.isEmpty(productInfoUpdate.getMainPic())) {
                                    productInfoUpdate.setMainPic(productInfoUpdate.getMainPic().replace(item, newUrl));
                                }

                                if (!StringUtils.isEmpty(productDetail1Update.getPicture())) {
                                    productDetail1Update.setPicture(productDetail1Update.getPicture().replace(item, newUrl));
                                }

                                if (!StringUtils.isEmpty(variantCompose)) {

                                    productDetail1Update.setVariantCompose(variantCompose.replace(item, newUrl));
                                }
                            }
                        } catch (Exception e) {
                            LOGGER.error("图片处理异常", e);
                        }
                    });

                    // 更新
                    productInfoMapper.updateByPrimaryKeySelective(productInfoUpdate);

                    productDetailMapper.updateByPrimaryKeySelective(productDetail1Update);
                } catch (Exception e) {
                    LOGGER.error("保存图片异常", e);
                }
            }
        });
    }

    private void executeCollectBatch(int userId, int orgId, String category, String categoryId, String sourceUrl) {
    }


    /**
     * 解析产品详情页
     *
     * @param document
     * @param productInfo
     * @param productDetail
     * @param imageUrlQiniu
     */
    private void parser1688Product(Document document, ProductInfo productInfo, ProductDetail productDetail, Set<String> imageUrlQiniu) {

        try {
            //图片
            Element picElement = document.selectFirst("#dt-tab > div > ul");
            List<Element> picElements = picElement.getElementsByTag("li");

            List<String> imgs = new ArrayList<>();
            picElements.forEach(item -> {
//                String imgUrl = item.getElementsByTag("img").attr("src");
//                imgUrl = handle1688ImgUrl(imgUrl);

                JSONObject data = JSONObject.fromObject(item.attr("data-imgs"));
                String imgUrl = data.optString("original");
                if (StringUtils.isEmpty(imgUrl)) {
                    Element paramImg = item.getElementsByTag("img") != null ? item.getElementsByTag("img").first() : null;
                    if (paramImg != null) {
                        imgUrl = paramImg.attr("src");
                        if (StringUtils.isEmpty(imgUrl) || (StringUtils.isNotEmpty(imgUrl) && imgUrl.contains("lazyload"))) {
                            imgUrl = paramImg.attr("data-lazy-src");
                        }
                    }
                }

                if (StringUtils.isNotEmpty(imgUrl)) {
                    String resultImg = handle1688ImgUrl(imgUrl);
                    imgs.add(resultImg);
                    imageUrlQiniu.add(resultImg);
                }
            });
            productDetail.setPicture(StringUtils.join(imgs.toArray(), ","));
        } catch (Exception e) {
            LOGGER.error("处理图片", e);
        }

        JSONArray variantArr = new JSONArray();

        // 生成变体组合用
        JSONArray variantDetailArr = new JSONArray();

        String mainVariant = "";

        try {
            JSONObject variant = new JSONObject();
            // 颜色变体
            Element colorElement = document.selectFirst("#mod-detail-bd > div.region-custom.region-detail-property.region-takla.ui-sortable.region-vertical > div.widget-custom.offerdetail_ditto_purchasing > div > div > div > div.obj-leading");
            String header = colorElement.getElementsByClass("obj-header").first().getElementsByClass("obj-title").text().trim();

            mainVariant = handleVariant(header);
            variant.put("variant", mainVariant);

            List<String> params = new ArrayList<>();

            JSONArray detailArr = new JSONArray();
            JSONObject nameAndPic = new JSONObject();

            List<Element> colorParams = colorElement.getElementsByClass("obj-content").first().getElementsByTag("ul").first().getElementsByTag("li");
            colorParams.forEach(item -> {
                Element paramDetail = item.getElementsByTag("div").first();
                JSONObject data = JSONObject.fromObject(paramDetail.attr("data-unit-config"));
                if (data == null) {
                    data = new JSONObject();
                }
                String name = data.optString("name");

                if (StringUtils.isEmpty(name)) {
                    name = data.optString("skuName");
                }


                params.add(name);
                String pic = data.optString("original");
                if (StringUtils.isEmpty(pic)) {
                    Element paramImg = paramDetail.getElementsByTag("img") != null ? paramDetail.getElementsByTag("img").first() : null;
                    if (paramImg != null) {
                        pic = paramImg.attr("src");
                        if (StringUtils.isEmpty(pic) || (StringUtils.isNotEmpty(pic) && pic.contains("lazyload"))) {
                            pic = paramImg.attr("data-lazy-src");
                        }
                    }
                }
                Integer stock = data.optInt("max", 100);
                nameAndPic.put("name", name);
                nameAndPic.put("type", handleVariant(header));
                nameAndPic.put("stock", stock);

                if (StringUtils.isNotEmpty(pic)) {
                    String resultImg = handle1688ImgUrl(pic);
                    nameAndPic.put("pic", resultImg);
                    imageUrlQiniu.add(resultImg);
                }

                detailArr.add(nameAndPic);
            });

            variant.put("args", StringUtils.join(params, ","));
//            variant.put("detail", detailArr);
            if (detailArr.size() > 0) {
                variantDetailArr.add(detailArr);
            }
            variantArr.add(variant);

        } catch (Exception e) {
            LOGGER.error("处理主要变体", e);
        }

        try {
            // 其他变体
            List<Element> otherElements = document.getElementsByClass("obj-sku");
            if (CollectionUtils.isNotEmpty(otherElements)) {
                otherElements = otherElements.subList(0, 1);
            }

            final String newMainVariant = mainVariant;
            otherElements.forEach(item -> {
                JSONObject variant = new JSONObject();
                String header = item.getElementsByClass("obj-header").first().getElementsByClass("obj-title").text().trim();

                String otherVariant = handleVariant(header);
                if (StringUtils.isNotEmpty(newMainVariant) && newMainVariant.equals(otherVariant)) {
                    if (newMainVariant.equals("color")) {
                        otherVariant = "size";
                    } else if (newMainVariant.equals("size")) {
                        JSONObject colorVariant = variantArr.getJSONObject(0);
                        colorVariant.put("variant", "color");
                        variantArr.set(0, colorVariant);

                        JSONArray newDetailArr = new JSONArray();
                        JSONArray oldDetailArr = variantDetailArr.getJSONArray(0);
                        for (Object o : oldDetailArr) {
                            JSONObject itemNameAndPic = JSONObject.fromObject(o);
                            itemNameAndPic.put("type", "color");
                            newDetailArr.add(itemNameAndPic);
                        }

                        variantDetailArr.set(0, newDetailArr);
                    }
                }
                variant.put("variant", otherVariant);

                List<String> params = new ArrayList<>();

                JSONArray detailArr = new JSONArray();
                JSONObject nameAndPic = new JSONObject();
                nameAndPic.put("stock", 100);

                List<Element> otherParams = item.getElementsByClass("obj-content").first().getElementsByTag("table").first().getElementsByTag("tbody").first().getElementsByTag("tr");
                otherParams.forEach(item1 -> {
                    Element paramDetail = item1.getElementsByTag("td").first().getElementsByTag("span").first();

                    String name = paramDetail.attr("title");
                    // 有图片的情况
                    if (!StringUtils.isEmpty(paramDetail.attr("title"))) {
                        nameAndPic.put("name", name.trim());
                        nameAndPic.put("type", handleVariant(header));
                        String pic = JSONObject.fromObject(Optional.ofNullable(paramDetail.attr("data-imgs")).orElse("{}")).optString("original");

                        if (StringUtils.isEmpty(pic)) {
                            Element paramImg = paramDetail.getElementsByTag("img") != null ? paramDetail.getElementsByTag("img").first() : null;
                            if (paramImg != null) {
                                pic = paramImg.attr("src");
                                if (StringUtils.isEmpty(pic) || (StringUtils.isNotEmpty(pic) && pic.contains("lazyload"))) {
                                    pic = paramImg.attr("data-lazy-src");
                                }
                            }
                        }

                        if (StringUtils.isNotEmpty(pic)) {
                            String resultImg = handle1688ImgUrl(pic);
                            nameAndPic.put("pic", resultImg);

                            imageUrlQiniu.add(resultImg);
                        }

                        detailArr.add(nameAndPic);
                        // 没有图片的情况
                    } else {
                        name = paramDetail.text().trim();
                        if (!StringUtils.isEmpty(name)) {
                            nameAndPic.put("name", name);
                            detailArr.add(nameAndPic);

                            params.add(name);
                        }
                    }

                });

                variant.put("args", StringUtils.join(params, ","));
                // variant.put("detail", detailArr);
                if (detailArr.size() > 0) {
                    variantDetailArr.add(detailArr);
                }
                variantArr.add(variant);
            });

        } catch (Exception e) {
            LOGGER.error("处理变体", e);
        }


        // 特殊处理变体相同的情况
        if (variantArr.size() > 1 && variantArr.getJSONObject(0).optString("variant").equals(variantArr.getJSONObject(1).optString("variant"))) {
            String sameVariant = variantArr.getJSONObject(0).optString("variant");
            if ("color".equals(sameVariant)) {
                JSONObject variantSize = variantArr.getJSONObject(1);
                variantSize.put("variant", "size");
                variantArr.set(1, variantSize);
            } else if ("size".equals(sameVariant)) {
                JSONObject variantColor = variantArr.getJSONObject(0);
                variantColor.put("variant", "color");
                variantArr.set(0, variantColor);
            }
        }

        productDetail.setVariant(variantArr.toString());

        try {
            StringBuffer sb = new StringBuffer();

            // 属性
            Element attrElement = document.selectFirst("#mod-detail-attributes > div.obj-content > table > tbody");

            List<Element> trElements = attrElement.getElementsByTag("tr");

            trElements.forEach(item -> {
                List<Element> tdElements = item.getElementsByTag("td");
                // 如果flag为true，则为标题，否则为值
                boolean flag = true;

                for (Element item2 : tdElements) {
                    String name = item2.text().trim();

                    if (!StringUtils.isEmpty(name)) {
                        if (flag) {
                            sb.append(name).append(": ");
                        } else {
                            sb.append(name).append("\r\n");
                        }
                    }

                    flag = !flag;

                }

            });

            productDetail.setDescriptionZh(sb.toString());


        } catch (Exception e) {
            LOGGER.error("处理属性", e);
        }

        try {
            // 标题
            Element titleElement = document.selectFirst("#mod-detail-title > h1");
            String title = titleElement.text().trim();

            productDetail.setTitleZh(title);
        } catch (Exception e) {
            LOGGER.error("处理标题", e);
        }

        try {
            // 价格
            Element priceElement = document.selectFirst("#mod-detail-price > div > table > tbody > tr.price");
            String priceText = priceElement.getElementsByTag("td").get(1).getElementsByTag("span").get(1).text();
            Integer price = (int) (Double.valueOf(priceText) * 100);

            productInfo.setCostPrice(price);
        } catch (Exception e) {
            LOGGER.error("处理价格", e);
        }

        String variantCompose = getVariantCompose(variantDetailArr);
        productDetail.setVariantCompose(variantCompose);

    }

    private void parserTaobaoProduct(Document document, ProductInfo productInfo, ProductDetail productDetail, Set<String> imageUrlQiniu) {

        try {
            //图片
            Element picElement = document.selectFirst("#J_UlThumb");
            List<Element> picElements = picElement.getElementsByTag("img");

            List<String> imgs = new ArrayList<>();
            picElements.forEach(item -> {
//                String imgUrl = item.getElementsByTag("img").attr("src");
//                imgUrl = handle1688ImgUrl(imgUrl);

                String imgUrl = item.attr("data-src");
                if (StringUtils.isEmpty(imgUrl)) {
                    imgUrl = item.attr("src");
                }

                if (StringUtils.isNotEmpty(imgUrl)) {
                    String resultImg = handleTaobaoImgUrl(imgUrl);
                    imgs.add(resultImg);
                    imageUrlQiniu.add(resultImg);
                }
            });
            productDetail.setPicture(StringUtils.join(imgs.toArray(), ","));
        } catch (Exception e) {
            LOGGER.error("处理图片", e);
        }

        JSONArray variantArr = new JSONArray();

        // 生成变体组合用
        JSONArray variantDetailArr = new JSONArray();

        try {
            // 变体
            Element element = document.selectFirst("#J_isku > div");

            List<Element> elements = element.getElementsByClass("J_Prop");
            if (CollectionUtils.isNotEmpty(elements)) {
                elements.forEach(item -> {
                    JSONObject variant = new JSONObject();
                    String variantCategory = item.getElementsByTag("ul").first().attr("data-property");

                    if (StringUtils.isNotEmpty(variantCategory.trim())) {
                        variant.put("variant", variantCategory.trim());

                        List<Element> liElements = item.getElementsByTag("ul").first().getElementsByTag("li");

                        JSONArray detailArr = new JSONArray();
                        if (CollectionUtils.isNotEmpty(liElements)) {
                            List<String> variantParams = new ArrayList<>();
                            liElements.forEach(p -> {
                                String variantParam = p.getElementsByTag("span").first().text();
                                variantParams.add(variantParam.trim());

                                JSONObject nameAndPic = new JSONObject();
                                nameAndPic.put("name", variantParam.trim());
                                nameAndPic.put("type", handleVariant(variantCategory));
                                nameAndPic.put("stock", 100);

                                Element aElement = p.getElementsByTag("a").first();
                                if (aElement != null && aElement.attr("style").contains("(") && aElement.attr("style").contains(")")) {
                                    String style = aElement.attr("style");
                                    String pic = handleTaobaoImgUrl(style.substring(style.indexOf("(") + 1, style.indexOf(")")));
                                    if (StringUtils.isNotEmpty(pic)) {
                                        nameAndPic.put("pic", pic);
                                        imageUrlQiniu.add(pic);
                                    }
                                }


                                detailArr.add(nameAndPic);
                            });

                            if (CollectionUtils.isNotEmpty(detailArr)) {
                                variantDetailArr.add(detailArr);
                                variant.put("args", StringUtils.join(variantParams, ","));
                                variantArr.add(variant);
                            }
                        }
                    }
                });
            }
        } catch (Exception e) {
            LOGGER.error("处理主要变体", e);
        }

        // 将多个变体处理为颜色尺寸两种变体
        JSONArray newVariantArr = new JSONArray();
        if (CollectionUtils.isNotEmpty(variantArr)) {
            for (Object o : variantArr) {
                JSONObject data = JSONObject.fromObject(o);
                if (CollectionUtils.isNotEmpty(newVariantArr)) {

                    String type = handleVariant(data.getString("variant"));
                    boolean handleFlag = false;

                    int i = 0;
                    for (Object o1 : newVariantArr) {
                        JSONObject data1 = JSONObject.fromObject(o1);
                        String type1 = data1.getString("variant");
                        if (type1.equals(type)) {

                            if (StringUtils.isNotEmpty(data1.optString("args"))) {
                                data1.put("args", data1.optString("args") + "," + data.optString("args"));
                            }
                            newVariantArr.set(i, data1);
                            handleFlag = true;
                            break;
                        }
                        i++;
                    }

                    if (!handleFlag) {
                        data.put("variant", type);
                        newVariantArr.add(data);
                    }

                } else {
                    data.put("variant", handleVariant(data.getString("variant")));
                    newVariantArr.add(data);
                }
            }
        }

        productDetail.setVariant(newVariantArr.toString());

        try {
            StringBuffer sb = new StringBuffer();

            // 属性
            Element attrElement = document.selectFirst("#attributes > ul");

            List<Element> liElements = attrElement.getElementsByTag("li");

            liElements.forEach(item -> {

                sb.append(item.text().trim()).append("\r\n");

            });

            productDetail.setDescriptionZh(sb.toString());


        } catch (Exception e) {
            LOGGER.error("处理属性", e);
        }

        try {
            // 标题
            Element titleElement = document.selectFirst("#J_Title > h3");
            String title = titleElement.text().trim();

            productDetail.setTitleZh(title);
        } catch (Exception e) {
            LOGGER.error("处理标题", e);
        }

        try {
            // 价格
            Element priceElement = document.selectFirst("#J_StrPrice");
            if (priceElement != null) {
                String priceText = priceElement.getElementsByTag("em").get(1).text().split("-")[0];
                Integer price = (int) (Double.valueOf(priceText) * 100);
                productInfo.setCostPrice(price);
            } else {
//                #J_PromoHd > div.tb-promo-item-bd > strong
                Element priceElement1 = document.getElementById("J_StrPrice");
                if (priceElement1 != null) {
                    String priceText = priceElement1.getElementsByTag("em").get(1).text().split("-")[0];
                    Integer price = (int) (Double.valueOf(priceText) * 100);
                    productInfo.setCostPrice(price);
                }
            }
        } catch (Exception e) {
            LOGGER.error("处理价格", e);
        }

        // 合并相同的变体
        JSONArray variantDetailArrNew = new JSONArray();
        JSONArray variantDetailArrColor = new JSONArray();
        JSONArray variantDetailArrSize = new JSONArray();

        if (CollectionUtils.isNotEmpty(variantDetailArr)) {
            for (Object o1 : variantDetailArr) {
                JSONArray dataArr = JSONArray.fromObject(o1);
                for (Object o : dataArr) {
                    JSONObject data = JSONObject.fromObject(o);
                    if (data.optString("type").equals("size")) {
                        variantDetailArrSize.add(data);
                    } else {
                        variantDetailArrColor.add(data);
                    }
                }
            }
        }

        if (CollectionUtils.isNotEmpty(variantDetailArrColor)) {
            variantDetailArrNew.add(variantDetailArrColor);
        }
        if (CollectionUtils.isNotEmpty(variantDetailArrSize)) {
            variantDetailArrNew.add(variantDetailArrSize);
        }

        String variantCompose = getVariantCompose(variantDetailArrNew);
        productDetail.setVariantCompose(variantCompose);
    }

    private void parserTmallProduct(Document document, ProductInfo productInfo, ProductDetail productDetail, Set<String> imageUrlQiniu) {

        try {
            //图片
            Element picElement = document.selectFirst("#J_UlThumb");
            List<Element> picElements = picElement.getElementsByTag("img");

            List<String> imgs = new ArrayList<>();
            picElements.forEach(item -> {
//                String imgUrl = item.getElementsByTag("img").attr("src");
//                imgUrl = handle1688ImgUrl(imgUrl);

                String imgUrl = item.attr("data-src");
                if (StringUtils.isEmpty(imgUrl)) {
                    imgUrl = item.attr("src");
                }

                if (StringUtils.isNotEmpty(imgUrl)) {
                    String resultImg = handleTmallImgUrl(imgUrl);
                    imgs.add(resultImg);
                    imageUrlQiniu.add(resultImg);
                }
            });
            productDetail.setPicture(StringUtils.join(imgs.toArray(), ","));
        } catch (Exception e) {
            LOGGER.error("处理图片", e);
        }

        JSONArray variantArr = new JSONArray();

        // 生成变体组合用
        JSONArray variantDetailArr = new JSONArray();

        try {
            // 变体
            Element element = document.selectFirst("#J_DetailMeta > div.tm-clear > div.tb-property > div > div.tb-key > div > div");

            List<Element> elements = element.getElementsByClass("tb-prop");
            if (CollectionUtils.isNotEmpty(elements)) {
                elements.forEach(item -> {
                    JSONObject variant = new JSONObject();
                    String variantCategory = item.getElementsByTag("ul").first().attr("data-property");

                    if (StringUtils.isNotEmpty(variantCategory.trim())) {

                        variant.put("variant", variantCategory.trim());

                        List<Element> liElements = item.getElementsByTag("ul").first().getElementsByTag("li");

                        JSONArray detailArr = new JSONArray();
                        if (CollectionUtils.isNotEmpty(liElements)) {
                            List<String> variantParams = new ArrayList<>();
                            liElements.forEach(p -> {
                                String variantParam = p.getElementsByTag("span").first().text();
                                variantParams.add(variantParam.trim());

                                JSONObject nameAndPic = new JSONObject();
                                nameAndPic.put("name", variantParam.trim());
                                nameAndPic.put("type", handleVariant(variantCategory));
                                nameAndPic.put("stock", 100);

                                Element aElement = p.getElementsByTag("a").first();
                                if (aElement != null && aElement.attr("style").contains("(") && aElement.attr("style").contains(")")) {
                                    String style = aElement.attr("style");
                                    String pic = handleTmall2ImgUrl(style.substring(style.indexOf("(") + 1, style.indexOf(")")));
                                    if (StringUtils.isNotEmpty(pic)) {
                                        nameAndPic.put("pic", pic);
                                        imageUrlQiniu.add(pic);
                                    }
                                }


                                detailArr.add(nameAndPic);
                            });

                            if (CollectionUtils.isNotEmpty(detailArr)) {
                                variantDetailArr.add(detailArr);
                                variant.put("args", StringUtils.join(variantParams, ","));
                                variantArr.add(variant);
                            }
                        }
                    }
                });
            }
        } catch (Exception e) {
            LOGGER.error("处理主要变体", e);
        }

        // 将多个变体处理为颜色尺寸两种变体
        JSONArray newVariantArr = new JSONArray();
        if (CollectionUtils.isNotEmpty(variantArr)) {
            for (Object o : variantArr) {
                JSONObject data = JSONObject.fromObject(o);
                if (CollectionUtils.isNotEmpty(newVariantArr)) {

                    String type = handleVariant(data.getString("variant"));
                    boolean handleFlag = false;

                    int i = 0;
                    for (Object o1 : newVariantArr) {
                        JSONObject data1 = JSONObject.fromObject(o1);
                        String type1 = data1.getString("variant");
                        if (type1.equals(type)) {

                            if (StringUtils.isNotEmpty(data1.optString("args"))) {
                                data1.put("args", data1.optString("args") + "," + data.optString("args"));
                            }
                            newVariantArr.set(i, data1);
                            handleFlag = true;
                            break;
                        }
                        i++;
                    }

                    if (!handleFlag) {
                        data.put("variant", type);
                        newVariantArr.add(data);
                    }

                } else {
                    data.put("variant", handleVariant(data.getString("variant")));
                    newVariantArr.add(data);
                }
            }
        }

        productDetail.setVariant(newVariantArr.toString());

        try {
            StringBuffer sb = new StringBuffer();

            // 属性
            Element attrElement = document.selectFirst("#J_AttrUL");

            List<Element> liElements = attrElement.getElementsByTag("li");

            liElements.forEach(item -> {

                sb.append(item.text().trim()).append("\r\n");

            });

            productDetail.setDescriptionZh(sb.toString());


        } catch (Exception e) {
            LOGGER.error("处理属性", e);
        }

        try {
            // 标题
            Element titleElement = document.selectFirst("#J_DetailMeta > div.tm-clear > div.tb-property > div > div.tb-detail-hd > h1");
            String title = titleElement.text().trim();

            productDetail.setTitleZh(title);
        } catch (Exception e) {
            LOGGER.error("处理标题", e);
        }

        try {
            // 价格
            Element priceElement = document.selectFirst("#J_PromoPrice > dd > div > span");
            if (priceElement != null) {
                String priceText = priceElement.text().split("-")[0];
                Integer price = (int) (Double.valueOf(priceText) * 100);
                productInfo.setCostPrice(price);
            } else {
//                #J_PromoHd > div.tb-promo-item-bd > strong
                Element priceElement1 = document.getElementsByClass("tm-promo-price").first();
                if (priceElement1 != null) {
                    String priceText = priceElement1.getElementsByTag("span").get(0).text().split("-")[0];
                    Integer price = (int) (Double.valueOf(priceText) * 100);
                    productInfo.setCostPrice(price);
                }
            }
        } catch (Exception e) {
            LOGGER.error("处理价格", e);
        }

        // 合并相同的变体
        JSONArray variantDetailArrNew = new JSONArray();
        JSONArray variantDetailArrColor = new JSONArray();
        JSONArray variantDetailArrSize = new JSONArray();

        if (CollectionUtils.isNotEmpty(variantDetailArr)) {
            for (Object o1 : variantDetailArr) {
                JSONArray dataArr = JSONArray.fromObject(o1);
                for (Object o : dataArr) {
                    JSONObject data = JSONObject.fromObject(o);
                    if (data.optString("type").equals("size")) {
                        variantDetailArrSize.add(data);
                    } else {
                        variantDetailArrColor.add(data);
                    }
                }
            }
        }

        if (CollectionUtils.isNotEmpty(variantDetailArrColor)) {
            variantDetailArrNew.add(variantDetailArrColor);
        }
        if (CollectionUtils.isNotEmpty(variantDetailArrSize)) {
            variantDetailArrNew.add(variantDetailArrSize);
        }

        String variantCompose = getVariantCompose(variantDetailArrNew);
        productDetail.setVariantCompose(variantCompose);
    }

    private void parserAmazonProduct(Document document, ProductInfo productInfo, ProductDetail productDetail, Set<String> imageUrlQiniu) {
        try {
            // 价格
            Element priceElement = document.selectFirst("#priceblock_saleprice");

            if (priceElement != null) {
                String priceText = priceElement.text().split("-")[0].trim().substring(1);
                Integer price = (int) (Double.valueOf(priceText) * 100);
                productInfo.setCostPrice(price);
            } else {

                Element priceElement2 = document.selectFirst("#priceblock_ourprice");
                if (priceElement2 != null) {
                    String priceText2 = priceElement2.text().substring(1);
                    Integer price2 = (int) (Double.valueOf(priceText2) * 100);
                    productInfo.setCostPrice(price2);
                } else {
//                #J_PromoHd > div.tb-promo-item-bd > strong
                    Element priceElement1 = document.selectFirst("#price > table > tbody > tr:nth-child(1) > td.a-span12.a-color-secondary.a-size-base > span.a-text-strike");

                    if (priceElement1 != null) {
                        String priceText1 = priceElement1.text().substring(1);
                        Integer price1 = (int) (Double.valueOf(priceText1) * 100);
                        productInfo.setCostPrice(price1);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("处理价格", e);
        }

        JSONArray variantArr = new JSONArray();

        // 生成变体组合用
        JSONArray variantDetailArr = new JSONArray();

        try {
            // 变体

            String variationValuesPattern = "(?<=\\\"variationValues\\\" : )(.*?)(?=,\\n)";
            Pattern p1 = Pattern.compile(variationValuesPattern);
            Matcher m1 = p1.matcher(document.toString());
            String variationValuesHtml = "";
            while (m1.find()) {
                variationValuesHtml = m1.group(0).trim();
            }

            String asinVariationValuesPattern = "(?<=\\\"asinVariationValues\\\" : )(.*?)(?=,\\n)";
            Pattern p2 = Pattern.compile(asinVariationValuesPattern);
            Matcher m2 = p2.matcher(document.toString());
            String asinVariationValuesHtml = "";
            while (m2.find()) {
                asinVariationValuesHtml = m2.group(0).trim();
            }

            if (StringUtils.isNotEmpty(variationValuesHtml) && StringUtils.isNotEmpty(asinVariationValuesHtml)) {
                JSONObject variationValuesJSON = JSONObject.fromObject(variationValuesHtml);
                JSONObject asinVariationValuesJSON = JSONObject.fromObject(asinVariationValuesHtml);

                /**
                 * variationValues" : {"size_name":["S fit UK6-8/ 32A 32B 32C 32D 32E 32F","M fit UK8-10/ 34A 34B 34C 34D 34E 34F","L fit UK12-14/ 36A 36B 36C 36D 36E","XL fit UK14-16/ 38A 38B 38C 38D 38E","XXL fit UK16-18/ 40A 40B 40C 40D","XXXL fit UK18-20/ 42A 42B 42C 42D"],"color_name":["1 Pack White #1","2 Pack Black+beige #1","2 Pack White+beige #1","2 Pack White+black #1","3 Pack White+black+beige #1","1 Pack Black #2","3 Pack Black Purple Beige #2"]},
                 "asinVariationValues" : {"B07CTF38PN":{"size_name":"1","ASIN":"B07CTF38PN","color_name":"0"},"B07CTG8FGL":{"size_name":"1","ASIN":"B07CTG8FGL","color_name":"1"},"B07CTHLGJW":{"size_name":"5","ASIN":"B07CTHLGJW","color_name":"3"},"B07CTH43ZZ":{"size_name":"3","ASIN":"B07CTH43ZZ","color_name":"4"},"B07CTGQRT5":{"size_name":"0","ASIN":"B07CTGQRT5","color_name":"2"},"B07CTFJM3B":{"size_name":"5","ASIN":"B07CTFJM3B","color_name":"2"},"B07CTF7SKW":{"size_name":"1","ASIN":"B07CTF7SKW","color_name":"4"},"B07FF3VRXX":{"size_name":"1","ASIN":"B07FF3VRXX","color_name":"5"},"B07CTFQCC9":{"size_name":"1","ASIN":"B07CTFQCC9","color_name":"3"},"B07CTF5JKK":{"size_name":"5","ASIN":"B07CTF5JKK","color_name":"0"},"B07CTHZJM8":{"size_name":"5","ASIN":"B07CTHZJM8","color_name":"1"},"B07CTGZ49L":{"size_name":"0","ASIN":"B07CTGZ49L","color_name":"4"},"B07CTGZ49H":{"size_name":"0","ASIN":"B07CTGZ49H","color_name":"3"},"B07DGCCW5G":{"size_name":"0","ASIN":"B07DGCCW5G","color_name":"6"},"B07CTJP1W4":{"size_name":"4","ASIN":"B07CTJP1W4","color_name":"1"},"B07CTFVQ96":{"size_name":"3","ASIN":"B07CTFVQ96","color_name":"1"},"B07CTF3867":{"size_name":"0","ASIN":"B07CTF3867","color_name":"1"},"B07CTGH1R4":{"size_name":"2","ASIN":"B07CTGH1R4","color_name":"1"},"B07CTFHNQ8":{"size_name":"3","ASIN":"B07CTFHNQ8","color_name":"0"},"B07CTFVC39":{"size_name":"4","ASIN":"B07CTFVC39","color_name":"3"},"B07CTGFDDZ":{"size_name":"2","ASIN":"B07CTGFDDZ","color_name":"2"},"B07CTFQWCK":{"size_name":"3","ASIN":"B07CTFQWCK","color_name":"2"},"B07CTDTTY9":{"size_name":"5","ASIN":"B07CTDTTY9","color_name":"4"},"B07DGBS3T3":{"size_name":"2","ASIN":"B07DGBS3T3","color_name":"6"},"B07DGTMWZ9":{"size_name":"1","ASIN":"B07DGTMWZ9","color_name":"6"},"B07CTG84G3":{"size_name":"1","ASIN":"B07CTG84G3","color_name":"2"},"B07FF2FH3N":{"size_name":"3","ASIN":"B07FF2FH3N","color_name":"5"},"B07CTCG3DQ":{"size_name":"2","ASIN":"B07CTCG3DQ","color_name":"0"},"B07DGDFL3S":{"size_name":"3","ASIN":"B07DGDFL3S","color_name":"6"},"B07CTFKKL4":{"size_name":"2","ASIN":"B07CTFKKL4","color_name":"3"},"B07CTG65JK":{"size_name":"4","ASIN":"B07CTG65JK","color_name":"0"},"B07CTG6CTF":{"size_name":"0","ASIN":"B07CTG6CTF","color_name":"0"},"B07CTHVWX1":{"size_name":"3","ASIN":"B07CTHVWX1","color_name":"3"},"B07FF3W6F4":{"size_name":"2","ASIN":"B07FF3W6F4","color_name":"5"},"B07CTHRRYK":{"size_name":"4","ASIN":"B07CTHRRYK","color_name":"2"},"B07CTDMLZV":{"size_name":"4","ASIN":"B07CTDMLZV","color_name":"4"},"B07FDVTFX5":{"size_name":"0","ASIN":"B07FDVTFX5","color_name":"5"}}
                 */

                // (?<=\"variationValues\" : )(.*?)(?=,\n)
//                    (?<=\"asinVariationValues\" : )(.*?)(?=,\n)
                JSONArray sizeArray = variationValuesJSON.optJSONArray("size_name");
                JSONArray colorArray = variationValuesJSON.optJSONArray("color_name");
                JSONArray styleArray = variationValuesJSON.optJSONArray("style_name");
                if (sizeArray != null && CollectionUtils.isNotEmpty(sizeArray)) {
                    variantArr.add(amazonVariant("size", sizeArray));
                }

                if (colorArray != null && CollectionUtils.isNotEmpty(colorArray) && styleArray != null && CollectionUtils.isNotEmpty(styleArray)) {
                    JSONArray newColorArray = JSONArray.fromObject(colorArray.toString());
                    newColorArray.addAll(styleArray);
                    variantArr.add(amazonVariant("color", newColorArray));
                } else {
                    if (colorArray != null && CollectionUtils.isNotEmpty(colorArray)) {
                        variantArr.add(amazonVariant("color", colorArray));
                    }
                    if (styleArray != null && CollectionUtils.isNotEmpty(styleArray)) {
                        variantArr.add(amazonVariant("color", styleArray));
                    }
                }

                JSONObject asinImage = amzonImages(document.toString(), imageUrlQiniu);

// [{"name":"白色##165*224","pic":"http://pic.dupf.xin/product/1011/1534339076385152.jpg","stock":100},{"name":"白色##224*224","pic":"http://pic.dupf.xin/product/1011/1534339076385152.jpg","stock":100},{"name":"白色##228*260","pic":"http://pic.dupf.xin/product/1011/1534339076385152.jpg","stock":100},{"name":"黑色##165*224","pic":"http://pic.dupf.xin/product/1011/1534339073851547.jpg","stock":100},{"name":"黑色##224*224","pic":"http://pic.dupf.xin/product/1011/1534339073851547.jpg","stock":100},{"name":"黑色##228*260","pic":"http://pic.dupf.xin/product/1011/1534339073851547.jpg","stock":100},{"name":"粉色##165*224","pic":"http://pic.dupf.xin/product/1011/1534339079204600.jpg","stock":100},{"name":"粉色##224*224","pic":"http://pic.dupf.xin/product/1011/1534339079204600.jpg","stock":100},{"name":"粉色##228*260","pic":"http://pic.dupf.xin/product/1011/1534339079204600.jpg","stock":100},{"name":"灰色##165*224","pic":"http://pic.dupf.xin/product/1011/1534339077779528.jpg","stock":100},{"name":"灰色##224*224","pic":"http://pic.dupf.xin/product/1011/1534339077779528.jpg","stock":100},{"name":"灰色##228*260","pic":"http://pic.dupf.xin/product/1011/1534339077779528.jpg","stock":100}]
                Iterator<String> iterator1 = asinVariationValuesJSON.keys();
                while (iterator1.hasNext()) {
                    String key1 = iterator1.next();
                    String value1 = asinVariationValuesJSON.getString(key1);
                    JSONObject jsonObject2 = JSONObject.fromObject(value1);

                    // 库存
                    jsonObject2.put("stock", 100);

                    // 图片
                    if (asinImage.containsKey(key1) && StringUtils.isNotEmpty(asinImage.getString(key1))) {
                        jsonObject2.put("pic", StringUtils.join(asinImage.getJSONArray(key1), ","));
                    }

                    // 价格
                    if (productInfo.getCostPrice() != null) {
                        jsonObject2.put("price", new BigDecimal(productInfo.getCostPrice()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP));
                    }

                    // 名称
                    if (jsonObject2.containsKey("color_name") && jsonObject2.containsKey("size_name")) {
                        jsonObject2.put("name", colorArray.get(jsonObject2.getInt("color_name")) + DELIMITER + sizeArray.get(jsonObject2.getInt("size_name")));
                    } else if (jsonObject2.containsKey("style_name") && jsonObject2.containsKey("size_name")) {
                        jsonObject2.put("name", styleArray.get(jsonObject2.getInt("style_name")) + DELIMITER + sizeArray.get(jsonObject2.getInt("size_name")));
                    } else if (jsonObject2.containsKey("color_name")) {
                        jsonObject2.put("name", colorArray.get(jsonObject2.getInt("color_name")));
                    } else if (jsonObject2.containsKey("style_name")) {
                        jsonObject2.put("name", styleArray.get(jsonObject2.getInt("style_name")));
                    } else if (jsonObject2.containsKey("size_name")) {
                        jsonObject2.put("name", sizeArray.get(jsonObject2.getInt("size_name")));
                    }

                    variantDetailArr.add(jsonObject2);

                    if (jsonObject2.containsKey("color_name") && jsonObject2.containsKey("style_name") && CollectionUtils.isNotEmpty(styleArray)) {
                        JSONObject itemStyle = JSONObject.fromObject(jsonObject2.toString());
                        itemStyle.put("color_name", itemStyle.optString("style_name"));
                        if (jsonObject2.containsKey("size_name") && CollectionUtils.isNotEmpty(sizeArray)) {
                            jsonObject2.put("name", styleArray.get(jsonObject2.getInt("style_name")) + DELIMITER + sizeArray.get(jsonObject2.getInt("size_name")));
                        } else {
                            jsonObject2.put("name", styleArray.get(jsonObject2.getInt("style_name")));
                        }
                        variantDetailArr.add(itemStyle);
                    }

                }

            } else {
                // 没有变体的情况采集图片
                amzonNoVariantImages(document.toString(), imageUrlQiniu);
            }
        } catch (Exception e) {
            LOGGER.error("处理主要变体", e);
        }

        productDetail.setVariant(variantArr.toString());

        productDetail.setVariantCompose(variantDetailArr.toString());

        try {
            StringBuffer sb = new StringBuffer();

            // 属性
            Element briefDescElement = document.selectFirst("#feature-bullets > ul");

            List<Element> descLiElements = briefDescElement.getElementsByTag("li");

            descLiElements.forEach(item -> {

                sb.append(item.getElementsByTag("span").first().text().trim()).append("\r\n");

            });

            productDetail.setBriefDescEn(sb.toString());


        } catch (Exception e) {
            LOGGER.error("处理属性", e);
        }

        try {
            StringBuffer sb = new StringBuffer();

            // 属性
//            Element attrElement = document.selectFirst("#productDetailsTable > tbody > tr > td > div.content > ul");
            Element attrElement = document.selectFirst("#productDescription > div > div");

            if(attrElement == null) {
                attrElement = document.selectFirst("#productDescription");
            }

            if(attrElement != null && StringUtils.isNotEmpty(attrElement.html())) {
                productDetail.setDescriptionEn(attrElement.html().trim());
            }

        } catch (Exception e) {
            LOGGER.error("处理属性", e);
        }

        try {
            // 标题
            Element titleElement = document.selectFirst("#productTitle");
            String title = titleElement.text().trim();

            productDetail.setTitleEn(title);
        } catch (Exception e) {
            LOGGER.error("处理标题", e);

        }

        try {
            //图片
            if(CollectionUtils.isNotEmpty(imageUrlQiniu)) {
                productDetail.setPicture(StringUtils.join(imageUrlQiniu, ","));
            }
        } catch (Exception e) {
            LOGGER.error("处理图片", e);
        }


    }

    private JSONObject amzonImages(String html, Set<String> imageUrlQiniu) {
        JSONObject result = new JSONObject();

        try {

            String pattern1 = "(?<=P.when\\('A', 'jQuery', 'ImageBlockATF', 'cf'\\).register\\('ImageBlockBTF', function\\(A, \\$, imageBlockATF, cf\\)\\{\\nvar data \\= \\{\\};\\nvar obj \\= jQuery.parseJSON\\(')(.*?)(?='\\);)";
            Pattern p1 = Pattern.compile(pattern1);
            Matcher m1 = p1.matcher(html);
            String imageHtml = "";
            while (m1.find()) {
                imageHtml = m1.group(0).trim();
            }
            if (StringUtils.isNotEmpty(imageHtml)) {
                JSONObject imageJSON = JSONObject.fromObject(imageHtml);
                if (imageJSON.containsKey("colorImages") && StringUtils.isNotEmpty(imageJSON.optString("colorImages"))) {
//                    (?<=P.when\('A', 'jQuery', 'ImageBlockATF', 'cf'\).register\('ImageBlockBTF', function\(A, \$, imageBlockATF, cf\)\{\nvar data \= \{\};\nvar obj \= jQuery.parseJSON\(')(.*?)(?='\);)
                    /**
                     * "colorToAsin": {
                     "Standing Wooden Tallboy Unit": {
                     "asin": "B01LY872JU"
                     },
                     "Standing Tallboy Unit": {
                     "asin": "B01LXKTFMZ"
                     },
                     "Modern": {
                     "asin": "B01AM8R37C"
                     }

                     "colorImages": {
                     "Standing Wooden Tallboy Unit": [{
                     "large": "https://images-na.ssl-images-amazon.com/images/I/31hh-HkpcQL.jpg",
                     "thumb": "https://images-na.ssl-images-amazon.com/images/I/31hh-HkpcQL._SS40_.jpg",
                     "hiRes": "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SL1000_.jpg",
                     "variant": "MAIN",
                     "main": {
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SX425_.jpg": ["425", "425"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SX522_.jpg": ["522", "522"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SX466_.jpg": ["466", "466"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SX679_.jpg": ["679", "679"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SY450_.jpg": ["450", "450"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SX569_.jpg": ["569", "569"],
                     "https://images-na.ssl-images-amazon.com/images/I/51eRqqGMGgL._SY355_.jpg": ["355", "355"]
                     }
                     }, {
                     "large": "https://images-na.ssl-images-amazon.com/images/I/31wcCW20JFL.jpg",
                     "thumb": "https://images-na.ssl-images-amazon.com/images/I/31wcCW20JFL._SS40_.jpg",
                     "hiRes": "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SL1000_.jpg",
                     "variant": "PT01",
                     "main": {
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SY355_.jpg": ["355", "355"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SX425_.jpg": ["425", "425"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SY450_.jpg": ["450", "450"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SX679_.jpg": ["679", "679"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SX522_.jpg": ["522", "522"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SX466_.jpg": ["466", "466"],
                     "https://images-na.ssl-images-amazon.com/images/I/41aD1Z388QL._SX569_.jpg": ["569", "569"]
                     }
                     }
                     ]
                     }
                     */
                    JSONObject colorToAsin = Optional.ofNullable(imageJSON.optJSONObject("colorToAsin")).orElse(new JSONObject());
                    JSONObject colorImages = Optional.ofNullable(imageJSON.optJSONObject("colorImages")).orElse(new JSONObject());

                    Iterator<String> iterator1 = colorToAsin.keys();
                    while (iterator1.hasNext()) {
                        String key1 = iterator1.next();
                        String value1 = colorToAsin.getString(key1);
                        JSONObject jsonObject2 = JSONObject.fromObject(value1);

                        String colorImage = colorImages.optString(key1, "{}");
                        JSONArray colorImageArray = JSONArray.fromObject(colorImage);
                        JSONArray resultImageArray = new JSONArray();
                        colorImageArray.forEach(item -> {
                            JSONObject itemJson = JSONObject.fromObject(item);
                            if(itemJson.containsKey("hiRes")){
                                resultImageArray.add(handleAmazonImgUrl(itemJson.optString("hiRes")));
                                imageUrlQiniu.add(handleAmazonImgUrl(itemJson.optString("hiRes")));
                            } else {
                                resultImageArray.add(itemJson.optString("large"));
                                imageUrlQiniu.add(itemJson.optString("large"));
                            }
                        });

                        String asin = jsonObject2.optString("asin");
                        result.put(asin, resultImageArray);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("处理亚马逊图片出错", e);
        }

        return result;
    }

    private JSONObject amzonNoVariantImages(String html, Set<String> imageUrlQiniu) {
        JSONObject result = new JSONObject();

        try {

//            String pattern1 = "(?<=P.when\\('A'\\).register\\()(.*?)(?=return data;)";
//            Pattern p1 = Pattern.compile(pattern1);
//            Matcher m1 = p1.matcher(html);
//            String imageHtml = "";
//            while (m1.find()) {
//                imageHtml = m1.group(0).trim();
//            }
//            if (StringUtils.isNotEmpty(imageHtml)) {
//
//                String pattern2 = "(?<=\"hiRes\":\")(.*?)(?=\",\")";
//                Pattern p2 = Pattern.compile(pattern2);
//                Matcher m2 = p2.matcher(imageHtml);
//                while (m2.find()) {
//                    String aa = m2.group();
//                    if(StringUtils.isNotEmpty(aa)) {
//                        imageUrlQiniu.add(handleAmazonImgUrl(aa.trim()));
//                    }
//                }
//            }

            String pattern2 = "(?<=\"hiRes\":\")(.*?)(?=\",\")";
            Pattern p2 = Pattern.compile(pattern2);
            Matcher m2 = p2.matcher(html);
            while (m2.find()) {
                String aa = m2.group();
                if(StringUtils.isNotEmpty(aa)) {
                    imageUrlQiniu.add(handleAmazonImgUrl(aa.trim()));
                }
            }
        } catch (Exception e) {
            LOGGER.error("处理亚马逊图片出错", e);
        }

        return result;
    }

    private JSONObject amazonVariant(String type, JSONArray args) {
        JSONObject data = new JSONObject();
        data.put("variant", type);
        data.put("args", args.toString());
        return data;
    }


    private String handleVariant(String header) {
        String result = "color";
        for (String con : SIZE_CONSTANT) {
            if (header.contains(con)) {
                result = "size";
            }
        }
        return result;
    }

    /**
     * 拼接变体组合
     *
     * @param variantArr
     * @return
     */
    private String getVariantCompose(JSONArray variantArr) {
        String result = null;

        if (variantArr == null || variantArr.size() <= 0) {
            return null;
        }

        if (variantArr.size() == 1) {
            result = variantArr.getJSONArray(0).toString();
        } else {

            JSONArray completeCompose = variantArr.getJSONArray(0);
//            for (int i = 0; i < completeCompose.size(); i++) {
            for (int j = 1; j < variantArr.size(); j++) {
                JSONArray waitCompose = variantArr.getJSONArray(j);

                completeCompose = doCompose(completeCompose, waitCompose);
            }
//            }
            result = completeCompose.toString();
        }

        return result;
    }

    /**
     * 组合变体
     *
     * @param completeCompose
     * @param waitCompose
     * @return
     */
    public JSONArray doCompose(JSONArray completeCompose, JSONArray waitCompose) {
        JSONArray newCompleteCompose = new JSONArray();
        for (int i = 0; i < completeCompose.size(); i++) {
            JSONObject completeItem = completeCompose.getJSONObject(i);
            for (int j = 0; j < waitCompose.size(); j++) {
                JSONObject waitItem = waitCompose.getJSONObject(j);

                JSONObject newItem = new JSONObject();

                String name = "";
                if (completeItem.optString("type", "").equals("color")) {
                    name = completeItem.optString("name") + DELIMITER + waitItem.optString("name");
                } else {
                    name = waitItem.optString("name") + DELIMITER + completeItem.optString("name");
                }

                Integer stock = completeItem.optInt("stock", 100);
                if (!stock.equals(100)) {
                    stock = waitItem.optInt("stock", 100);
                }

                String pic = null;
                if (!StringUtils.isEmpty(completeItem.optString("pic")) && !StringUtils.isEmpty(waitItem.optString("pic"))) {
                    pic = completeItem.optString("pic") + "," + waitItem.optString("pic");
                } else if (!StringUtils.isEmpty(completeItem.optString("pic"))) {
                    pic = completeItem.optString("pic");
                } else if (!StringUtils.isEmpty(waitItem.optString("pic"))) {
                    pic = waitItem.optString("pic");
                }

                newItem.put("name", name);
                newItem.put("pic", pic);
                newItem.put("stock", stock);
                newCompleteCompose.add(newItem);
            }
        }

        return newCompleteCompose;
    }

    /**
     * 处理1688的图片
     *
     * @param url
     * @return
     */
    public static String handle1688ImgUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }

        String pattern = "\\.\\d{2,}x\\d{2,}\\.";

        return url.replaceAll(pattern, ".");
    }


    /**
     * 处理淘宝的图片
     *
     * @param url
     * @return
     */
    public static String handleTaobaoImgUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }

        if (!url.startsWith("http")) {
            if (!url.startsWith("//")) {
                url = "http://" + url;
            } else {
                url = "http:" + url;
            }
        }

        String pattern = "_\\d{2,}x\\d{2,}\\.";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(url);
        while (m.find()) {
            return url.substring(0, url.indexOf(m.group(0)));
        }

        return url;
    }

    /**
     * 处理天猫的图片
     *
     * @param url
     * @return
     */
    public static String handleTmallImgUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }

        if (!url.startsWith("http")) {
            if (!url.startsWith("//")) {
                url = "http://" + url;
            } else {
                url = "http:" + url;
            }
        }

        String pattern = "_\\d{2,}x\\d{2,}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(url);
        while (m.find()) {
            return url.substring(0, url.indexOf(m.group(0)));
        }

        return url;
    }

    /**
     * 处理天猫的图片(处理变体中的图片)
     *
     * @param url
     * @return
     */
    public static String handleTmall2ImgUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }

        if (!url.startsWith("http:")) {
            if (!url.startsWith("//")) {
                url = "http://" + url;
            } else {
                url = "http:" + url;
            }
        }

        String pattern = "\\d{2,}x\\d{2,}";

        return url.replaceAll(pattern, "");
    }

    /**
     * 处理亚马逊的图片
     *
     * @param url
     * @return
     */
    public static String handleAmazonImgUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }

        if (!url.startsWith("http")) {
            if (!url.startsWith("//")) {
                url = "http://" + url;
            } else {
                url = "http:" + url;
            }
        }

        String pattern = "._(.*?)_";
        return url.replaceAll(pattern, "");
    }

    public static void main(String[] args) {
//        System.out.println(handle1688ImgUrl("https://cbu01.alicdn.com/img/ibank/2017/414/731/4174137414_1514371716.jpg"));
//        System.out.println(handle1688ImgUrl("https://cbu01.alicdn.com/img/ibank/2017/414/731/4174137414_1514371716.32x32.jpg"));
//
//        String str = "https://cbu01.alicdn.com/img/ibank/2017/414/731/4174137414_1514371716xxxxxx.jpg";
//        String pattern = "\\.\\d{2,}x\\d{2,}\\.";
//
//        System.out.println(str.replaceAll(pattern, "."));


        System.out.println(handleTaobaoImgUrl("http://gd4.alicdn.com/imgextra/i4/1961838038/TB2DDsZqQKWBuNjy1zjXXcOypXa_!!1961838038.jpg_50x50.jpg_.webp"));
    }

}